<template>
  <div class="fm-style">
    <a-form layout="horizontal" :key="formkey">
      <template v-for="item in data.list">
        <template v-if="item.type === 'grid'">
          <div :key="item.key">
            <a-row :gutter="item.options.gutter ? item.options.gutter : 0">
              <a-col
                v-for="(col, colIndex) in item.columns"
                :key="colIndex"
                :span="col.span"
                :offset="col.offset"
              >
                <template v-for="citem in col.list">
                  <genetate-form-item
                    v-if="citem.type !== 'grid'"
                    :key="citem.key"
                    :widget="citem"
                  ></genetate-form-item>
                </template>
              </a-col>
            </a-row>
          </div>
        </template>

        <template v-else>
          <genetate-form-item
            :key="item.key"
            :widget="item"
          ></genetate-form-item>
        </template>
      </template>
    </a-form>
  </div>
</template>

<script>
  import GenetateFormItem from './GenerateFormItem'
  import { Getsqlvalues } from '@/api/flow/api'

  export default {
    name: 'FmGenerateForm',
    components: {
      GenetateFormItem,
    },
    props: ['data'],

    data() {
      return {
        formkey: '',
      }
    },
    created() {
      // this.generateModle(this.data.list)
    },
    methods: {
      generateModle(genList) {
        var that = this
        for (let i = 0; i < genList.length; i++) {
          if (genList[i].type === 'grid') {
            genList[i].columns.forEach((item) => {
              this.generateModle(item.list)
            })
          } else {
            if (genList[i].options.remote === true) {
              // 通过sql查询数据
              Getsqlvalues({
                DbId: that.data.config.linkDb,
                Sql: genList[i].options.sql,
              }).then((res) => {
                if (res.isSuccess) {
                  genList[i].options.defaultValue = res.content
                  this.formkey = new Date().getTime()
                } else {
                  genList[i].options.defaultValue = []
                }
              })
            }
          }
        }
      },
    },
  }
</script>
